package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewMateriaMapper;
import com.usian.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FastDFSClient fastDFSClient;

    @Value("${fdfs.url}")
    private String baseUrl;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private WmNewMateriaMapper wmNewMateriaMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if (multipartFile == null) return errorParam;
        String s = null;
        try {
            s = fastDFSClient.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        WmMaterial material = new WmMaterial();
        material.setUserId(ThreadLocalUtils.getUserId());
        material.setUrl(s);
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setCreatedTime(new Date());
        save(material);
        return ResponseResult.okResult(baseUrl + s);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检查参数

        //2.带条件分页查询
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //获取当前登录的用户
        Integer uid = ThreadLocalUtils.getUserId();
        lambdaQueryWrapper.eq(WmMaterial::getUserId, uid);
        //是否收藏
        if (dto.getIsCollection() != null && dto.getIsCollection().shortValue() == 1) {
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        //按照日期倒序排序
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        IPage pageParam = new Page(dto.getPage(), dto.getSize());
        IPage resultPage = page(pageParam, lambdaQueryWrapper);
        //3.结果返回
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) resultPage.getTotal());
        List<WmMaterial> datas = resultPage.getRecords();
        //为每个图片加上前缀
        datas = datas.stream().map(item -> {
            item.setUrl(baseUrl + item.getUrl());
            return item;
        }).collect(Collectors.toList());
        responseResult.setData(datas);
        System.out.println(1);

        return responseResult;

    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if (id == null) return errorParam;
        WmMaterial material = getById(id);
        if (material == null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "您删除的素材不存在");
        Integer count = wmNewMateriaMapper.selectCount(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if (count > 0) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该素材已被引用");
        String url=material.getUrl();
        fastDFSClient.delFile(url);
        removeById(id);
        return success;
    }

    @Override
    public ResponseResult updateStatus(Integer id, Short type) {
        if (id==null)return errorParam;
        if (type==null||(type!=0&&type!=1))return errorParam;
        update(Wrappers.<WmMaterial>lambdaUpdate().set(WmMaterial::getIsCollection,type).eq(WmMaterial::getId
        ,id));
        return success;
    }
}

